<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>new.js</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../../../../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../../../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../../../../css/github.css" type="text/css" media="screen" />
<script src="../../../../../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>
    <div class="banner">
        
        <h1>
            new.js
        </h1>
        <ul class="files">
            
            <li>
                lib/assets/javascripts/sale_app/custom_products/new.js
                
            </li>
            <li>Last modified: 2017-02-04 19:48:23 +0800</li>
        </ul>
    </div>

    <div id="bodyContent">
        <div id="content">
  
    <div class="description">
      
<p>jQuery(function($){</p>

<pre><code>var BMS_CRM_OBJ = {}

BMS_CRM_OBJ.DELETE_IMAGE_ID = []

var SHOW_TYPE = &quot;&quot;

function getPageView() {
  if ($(&quot;#app-container&quot;).length &gt; 0 ) {
    return $(&quot;#app-container&quot;).attr(&quot;myView&quot;)
  }
}

function addImageContainer(num, type){
  console.log(&quot;这里传来新image空白的数字是&quot;+ num);
    var html =
            &quot;&lt;div class=&#39;fileinput fileinput-new&#39; data-provides=&#39;fileinput&#39;&gt;&quot; +
            &quot;&lt;div class=&#39;fileinput-preview thumbnail&#39; data-trigger=&#39;fileinput&#39;&gt;&lt;/div&gt;&quot; +
            &quot;&lt;div class=&#39;btn-submit-change&#39;&gt;&quot; +
            &quot;&lt;span class=&#39;btn btn-file&#39;&gt;&quot; +
            &quot;&lt;span class=&#39;fileinput-new iconfont&#39;&gt;&lt;i class=&#39;fa fa-file-image-o&#39; aria-hidden=&#39;true&#39;&gt;&lt;/i&gt;&lt;/span&gt;&quot; +
            &quot;&lt;span class=&#39;fileinput-exists iconfont&#39;&gt;&lt;i class=&#39;fa fa-file-image-o&#39; aria-hidden=&#39;true&#39;&gt;&lt;/i&gt;&lt;/span&gt;&quot;

        var identity = &quot;&quot;
        switch (type) {
            case &quot;store_product&quot;:
                identity = &quot;store_product[images_attributes]&quot;
                break
            case &quot;room&quot;:
                identity = &quot;store_product[images_attributes]&quot;
                break
            default:
                break
        }
        html += &quot;&lt;input class=&#39;upload-input upload-image&#39; type=&#39;file&#39; name=&#39;&quot; + identity + &quot;[&quot; + num + &quot;][image]&#39;&gt;&quot;+
            &quot;&lt;/span&gt;&quot; +
            &quot;&lt;/div&gt;&quot; +
            &quot;&lt;div class=&#39;btn-close iconfont&#39;&gt;&lt;i class=&#39;fa fa-trash-o&#39; aria-hidden=&#39;true&#39;&gt;&lt;/i&gt;&lt;/div&gt;&quot; +
            &quot;&lt;/div&gt;&quot;
        $(&quot;.mssUploadFileContainer&quot;).append(html)
} //end addImageContainer

BMS_CRM_OBJ.whichImageContainer = function(num) {
  if ( getPageView() == &quot;ROOM-EDIT-PAGE&quot; ) { // 产品
      addImageContainer(num,&quot;store_product&quot;)
  }
}

if ( $(&quot;.mssUploadFileContainer&quot;).length &gt; 0 ) {
  var max_count = 3
  var current_img_count = parseInt($(&quot;.mssUploadFileContainer&quot;).attr(&quot;data-image-num&quot;))
  if ( $(&quot;.mssUploadFileContainer[data-image-num]&quot;).length &gt; 0 &amp;&amp; current_img_count &lt; max_count )
   {
      BMS_CRM_OBJ.whichImageContainer(current_img_count) // 新增空白图片（选择图片类型）
    }

  // 点击新增按钮（针对新增和修改页面）
    $(document).on(&quot;click&quot;,&quot;.fileinput .btn-file&quot;,function () {
        $(&quot;.fileinput .btn-file input&quot;).change(function (event) {

            var current_img_count_tmp = 0
            $(&quot;.mssUploadFileContainer .fileinput .fileinput-preview img&quot;).each(function () {
                if ( $(this).attr(&quot;src&quot;) != &quot;&quot; ) {
                    current_img_count_tmp++
                }
            })

            var img_fill_count = $(&quot;.mssUploadFileContainer .fileinput&quot;).length

            if ( img_fill_count - current_img_count_tmp &gt; 1 ) {
                // $(this).
            } else {
                console.log($(this).parents(&quot;.fileinput&quot;).find(&quot;.fileinput-preview img&quot;).length);
                if ( $(this).parents(&quot;.fileinput&quot;).find(&quot;.fileinput-preview img&quot;).length == 0 &amp;&amp; img_fill_count &lt; max_count) {
                    if ( getPageView() == &quot;ROOM-EDIT-PAGE&quot; || getPageView() == &quot;ROOM-NEW-PAGE&quot; ) {
                        addImageContainer(img_fill_count, &quot;room&quot;)
                    }
                    // 这里可以用length+1获取, 也可以直接获取最后一个
                    $(&quot;.mssUploadFileContainer .fileinput:nth-child(&quot; + (img_fill_count + 1) + &quot;)&quot;).show()
                }
            }

        })
    })
    // end 点击新增按钮

    // 点击删除按钮（只针对于修改页面）
    $(&quot;.mssUploadFileContainer&quot;).on(&quot;click&quot;,&quot;.btn-close&quot;,function () {
        var max_count = 3
        var img_total_count = $(&quot;.mssUploadFileContainer .fileinput&quot;).length // 图片容器个数,包括最后一张空的图片
        var click_index = $(this).parent().index()
        var activity_id // 客房或者餐饮等的id
        var delete_image_id // 删除的图片的id
        if ( getPageView() == &quot;ROOM-EDIT-PAGE&quot;) {
            console.log(&quot;first&quot;);
            activity_id = $(&quot;#app-container[myView=&#39;ROOM-EDIT-PAGE&#39;] #EditView&quot;).attr(&quot;data-id&quot;)
        }
        delete_image_id = $(this).parent().attr(&quot;delete-image-id&quot;) // 被删除图片的id

        BMS_CRM_OBJ.DELETE_ID = activity_id // 当前修改的id

        if ( (click_index + 1) &lt; img_total_count &amp;&amp; img_total_count &gt;1 &amp;&amp; img_total_count &lt; max_count ) {
          console.log(&quot;一　１&quot; );
          console.log(img_total_count - 2);
            $(this).parent().remove() // 移除图片
            BMS_CRM_OBJ.DELETE_IMAGE_ID.push(delete_image_id)
            BMS_CRM_OBJ.editLastImageIndex( img_total_count-2 )
        }
        else if ( (click_index + 1) &lt; img_total_count &amp;&amp; img_total_count == max_count ) {
            if ( $(&quot;.mssUploadFileContainer .fileinput:nth-child(6) .thumbnail img&quot;).length &gt; 0) {
              console.log(&quot;二 1&quot;+ img_total_count);
                $(this).parent().remove() // 移除图片
                BMS_CRM_OBJ.DELETE_IMAGE_ID.push(delete_image_id)

                BMS_CRM_OBJ.whichImageContainer(img_total_count) // 新增空白图片（选择图片类型）
                BMS_CRM_OBJ.editLastImageIndex( img_total_count-1 )

            } else {
              console.log(&quot;二 2&quot;+ img_total_count);
                $(this).parent().remove() // 移除图片
                BMS_CRM_OBJ.DELETE_IMAGE_ID.push(delete_image_id)
                BMS_CRM_OBJ.editLastImageIndex( img_total_count-2 )
            }
        }
        else if ( (click_index + 1) == img_total_count &amp;&amp; img_total_count == max_count ) {
            if ( $(&quot;.mssUploadFileContainer .fileinput:nth-child(&quot; + max_count + &quot;) .thumbnail img&quot;).length &gt; 0) {
              console.log(&quot;三 1&quot;+ img_total_count);
                $(this).parent().remove() // 移除图片
                BMS_CRM_OBJ.DELETE_IMAGE_ID.push(delete_image_id)

                BMS_CRM_OBJ.whichImageContainer(img_total_count) // 新增空白图片（选择图片类型）
                BMS_CRM_OBJ.editLastImageIndex( img_total_count-1 )
            }

        }

    })
    // end 点击删除按钮
} // if 判断结束

BMS_CRM_OBJ.editLastImageIndex = function (num) {
  if ( getPageView() == &quot;ROOM-EDIT-PAGE&quot; ) { // 客房
      $(&quot;.mssUploadFileContainer .fileinput:last-child .upload-image&quot;).attr(&quot;name&quot;,&quot;store_product[images_attributes][&quot; + num + &quot;][image]&quot;)
  }
}

  //点击是否优惠价
  $(document).on(&quot;change&quot;, &quot;div#app-container[myApp=&#39;ROOM-NEW-EDIT-PAGE&#39;] select.js-vipable-change&quot;, function(){
    var status = $(this).val();
    $(&quot;div.js-vipprice-change&quot;).toggle();
  })

  // button commit in edit page
  if ( getPageView() == &quot;ROOM-EDIT-PAGE&quot; ) {
    $(document).on(&quot;click&quot;,&quot;#app-container[myApp=&#39;ROOM-NEW-EDIT-PAGE&#39;] .button-submit-cancle .button-submit&quot;,function () {
      // 客房编辑页面删除图片
      if (BMS_CRM_OBJ.DELETE_ID != undefined &amp;&amp; BMS_CRM_OBJ.DELETE_IMAGE_ID.length &gt; 0) {
        // 如果已经点击删除按钮(至少删除一副图片)
        var csrf = $(&quot;input#js-ruiqu-store-product-edit&quot;).val();
        $.ajax({
          url: &quot;/ruiqu/store/products/&quot;+ BMS_CRM_OBJ.DELETE_ID +&quot;/remove_image&quot;,
          type: &quot;delete&quot;,
          dataType: &quot;json&quot;,
          headers: {
                    &#39;X-CSRF-Token&#39;: csrf
                  },
          data: {image_ids: BMS_CRM_OBJ.DELETE_IMAGE_ID},
          success: function (data) {
              console.log(&quot;删除成功&quot;)
          },
          error: function () {
              console.log(&quot;删除失败&quot;)
          }
        })
      }
    })
  }</code></pre>

<p>})</p>

    </div>
  


  


  
  


  


  

  



  

    

    

    


    


    <!-- Methods -->
              </div>

    </div>
  </body>
</html>
